﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;
using System.Windows.Controls.Primitives;
using System.Collections.Generic;

namespace Tsina
{
    public class IMEBehavior : Behavior<TextBox>
    {
        private static bool IsValidPinyinKey(Key key)
        { 
            int keyNumber = (int)key;
            if ((keyNumber >= 30 && keyNumber <= 55))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public IMEBehavior()
        {
            imePopup = new Popup();
            imePanel = new IMEControl();
            imePanel.ManipulationStarted += imePanel_ManipulationStarted;
            imePanel.GotHanzi += imePanel_GotHanzi;
            imePopup.Child = imePanel;
            imePopup.VerticalOffset = 339;
        }

        private void imePanel_GotHanzi(object sender, EventArgs e)
        {
            AssociatedObject.Text += (sender as IMEButtonControl).Text;
            AssociatedObject.Select(AssociatedObject.Text.Length, 0);
        }

        private void imePanel_ManipulationStarted(object sender, ManipulationStartedEventArgs e)
        {
            e.Complete();
        }

        private bool isActive;

        private Popup imePopup;

        private IMEControl imePanel;

        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.GotFocus += Target_GotFocus;
            AssociatedObject.LostFocus += Target_LostFocus;
            AssociatedObject.KeyDown += Target_KeyDown;
            AssociatedObject.KeyUp += Target_KeyUp;
        }

        private void Target_GotFocus(object sender, RoutedEventArgs e)
        {
            if (isActive == true)
            {
                imePopup.IsOpen = true;
            }
        }

        private void Target_LostFocus(object sender, RoutedEventArgs e)
        {
            if (isActive == true)
            {
                imePopup.IsOpen = false;
            }
        }

        private void Target_KeyDown(object sender, KeyEventArgs e)
        {
            if (isActive == true)
            {
                e.Handled = true;
                if (IsValidPinyinKey(e.Key) == true)
                {
                    imePanel.Pinyin += e.Key.ToString().ToLower();
                }
                if (e.Key == Key.Enter)
                {
                    isActive = false;
                    imePopup.IsOpen = false;
                }
            }
            else
            {
                if (e.Key == Key.Enter)
                {
                    isActive = true;
                    imePopup.IsOpen = true;
                }
            }
        }

        private void Target_KeyUp(object sender, KeyEventArgs e)
        {
            
        }
    }
}
